package com.aiju.cash.ui.multiple_shops.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Shop;
import com.aiju.bean.ShopClerk;
import com.aiju.cash.R;
import com.aiju.cash.core.api.ShopManagetApi;
import com.aiju.cash.core.event.ClerkAddEvent;
import com.aiju.cash.ui.multiple_shops.adapter.ShopClerkAdapter;
import com.aiju.cash.util.GsonHelper;
import com.aiju.common.PixValue;
import com.aiju.common.event.EventBusHandler;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.T;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-06-13 10:03
 * @version: V1.0
 * @Description: 店员列表
 */
public class ClerkActivity extends BaseTitleActivity {

    private List<ShopClerk> shopClerks;
    private ShopClerkAdapter clerkAdapter;
    private SwipeMenuListView mListView;
    private Shop mShop;
    private TextView tv_empty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clerk);
        setCenterTitle("店员列表");

        mShop = (Shop) getIntent().getSerializableExtra("shop");
        tv_empty = (TextView) findViewById(R.id.tv_empty);
        findViewById(R.id.btn_add_clerk).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ClerkActivity.this, ClerkAddActivity.class);
                intent.putExtra("shop", mShop);
                startActivity(intent);
            }
        });

        mListView = (SwipeMenuListView) findViewById(R.id.lv_clerk);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                deleteItem.setWidth(PixValue.dp.valueOf(50));
                deleteItem.setIcon(R.mipmap.icon_good_delete);
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        mListView.setMenuCreator(creator);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShopClerk shopClerk = shopClerks.get(position);
                Intent intent = new Intent(ClerkActivity.this, ClerkAddActivity.class);
                intent.putExtra("showType", ClerkAddActivity.SHOW_INFO);
                intent.putExtra("shopClerk", shopClerk);
                intent.putExtra("shop", mShop);
                startActivity(intent);
            }
        });
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        ShopClerk shopClerk = shopClerks.get(position);
                        if (!shopClerk.getRole().equals("0")) {
                            deleteClerk(shopClerks.get(position).getId());
                        } else {
                            T.ToastShort("不能删除总店长");
                        }
                        break;
                }
                return false;
            }
        });
        cherkList();

        EventCenter.bindContainerAndHandler(getLifecycle(), new EventBusHandler() {
            public void onEventMainThread(ClerkAddEvent event) {
                cherkList();
            }
        });
    }

    /**
     * 删除店员
     */
    private void deleteClerk(String id) {
        new ShopManagetApi(mDialogHelper).deleteClerk(id, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("成功删除店员");
                cherkList();
            }
        });
    }

    /**
     * 获取店员列表
     */
    private void cherkList() {
        new ShopManagetApi(mDialogHelper).clerkList(mShop.getStoreId(),new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                shopClerks = GsonHelper.convertEntities(data, ShopClerk.class);
                clerkAdapter = new ShopClerkAdapter(ClerkActivity.this, shopClerks);
                mListView.setAdapter(clerkAdapter);
                if (shopClerks.size() == 0) {
                    mListView.setVisibility(View.GONE);
                    tv_empty.setVisibility(View.VISIBLE);
                } else {
                    mListView.setVisibility(View.VISIBLE);
                    tv_empty.setVisibility(View.GONE);

                }
            }
        });
    }

}
